#ifndef datatrans_h__
#define datatrans_h__
#include <QThread>
#include <QTimer>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include "myevent.h"
#define WAIT_HEART_BEAT_MS 10000
#define BUFFER_SIZE 1024
#define NAME_SIZE 4
class QMainWindow;
class DataRecv;
class DataRecvThread : public QThread
{
public:
	DataRecvThread(QMainWindow* p) :transfer(0), data_types(),parent(p){}
	DataRecv* receiver(){ return transfer; }
    void addDataType(const QByteArray& name, int data_size, QWidget* p);
    void setDataType(const QByteArray& name, int data_size, QWidget* p);
	~DataRecvThread();
	QHash<QByteArray, DataType*> data_types; 
private:
	DataRecv* transfer;
	QMainWindow* parent;
	void run();
};
class DataRecv : public QObject
{
	Q_OBJECT
	friend class DataRecvThread;
public:
	quint16 localPort;
private slots:
	void recvData();
	void tcpConneted();
	void tcpDisconnected();
private:
	bool event(QEvent* e); 
	DataRecv(QHash<QByteArray, DataType*>* t, QMainWindow* p);
	~DataRecv();
	char* buffer;
	int next;
	int tid;
	QMainWindow* parent;
	QTcpServer* server;
	QTcpSocket* recv_client;
	QTcpSocket* heatbeat_client;
	QHash<QByteArray, DataType*>* table;
};
#endif // datatrans_h__
